﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace CinemaMaestro.WindowsApplication.EndPoints
{
    public class AuthEndPoints
    {
        private HttpClient client;

        public AuthEndPoints(HttpClient client)
        {
            this.client = client;
        }

        public async Task<string> GetAPIToken(string userName, string password)
        {
            var formContent = new FormUrlEncodedContent(new[]
            {
                new KeyValuePair<string, string>("grant_type", "password"), 
                new KeyValuePair<string, string>("username", userName), 
                new KeyValuePair<string, string>("password", password), 
            });

            HttpResponseMessage responseMessage = await client.PostAsync("/Token", formContent);
            var responseJson = await responseMessage.Content.ReadAsStringAsync();
            var jObject = JObject.Parse(responseJson);
            return jObject.GetValue("access_token").ToString();
        }

    }
}
